/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nxlv2.nomad.view;

import com.nxlv2.nomad.accessdb.services.UtilisateurServices;
import com.nxlv2.nomad.businessobjects.Client;
import com.nxlv2.nomad.businessobjects.Satisfaction;
import com.nxlv2.nomad.businessobjects.Utilisateur;
import com.nxlv2.nomad.view.ficheClient.CreateClient;
import com.nxlv2.nomad.view.ficheClient.FicheClientAction;
import com.nxlv2.nomad.view.ficheClient.SynchroAction;
import java.util.List;
import javax.swing.JList;

/**
 *
 * @author Casey
 */
public final class Welcome extends javax.swing.JFrame {

    private final Utilisateur user;
    private String clientSelect;

    public void setClientSelect(String clientSelect) {
        this.clientSelect = clientSelect;
    }

    /**
     * Creates new form Welcome
     */
    public Welcome(Utilisateur user) {
        initComponents();
        this.user = user;
        this.setListClient();
        this.setListSatisfaction();
        this.lblBonjour.setText("Bonjour " + user.getPrenom() + " " + user.getNom());
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        addClient = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        ficheClient = new javax.swing.JButton(new FicheClientAction(this));
        lblBonjour = new javax.swing.JLabel();
        btnSynchro = new javax.swing.JButton(new SynchroAction(this));
        jLabel1 = new javax.swing.JLabel();
        lblTitreDemande = new javax.swing.JLabel();
        lblTitreDemande1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabelTs = new javax.swing.JLabel();
        jLabelS = new javax.swing.JLabel();
        jLabelPeS = new javax.swing.JLabel();
        jLabelPaS = new javax.swing.JLabel();
        jLabelNbTotal = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Accueil");

        addClient.setText("Créer client");
        addClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addClientActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        ficheClient.setText("Fiche client");
        ficheClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ficheClientActionPerformed(evt);
            }
        });

        lblBonjour.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        btnSynchro.setText("Synchroniser");

        jLabel1.setText("Très satisfait :");

        lblTitreDemande.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblTitreDemande.setText("Clients");

        lblTitreDemande1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblTitreDemande1.setText("Satisfaction");

        jLabel2.setText("Satisfait :");

        jLabel3.setText("Pas satisfait :");

        jLabel4.setText("Peu satisfait :");

        jLabelTs.setText("jLabelTS");

        jLabelS.setText("jLabelS");

        jLabelPeS.setText("jLabelPeS");

        jLabelPaS.setText("jLabelPaS");

        jLabelNbTotal.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabelNbTotal.setText("jLabel5");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(lblBonjour, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(lblTitreDemande)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblTitreDemande1)
                        .addGap(143, 143, 143))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSynchro)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(addClient)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(ficheClient))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(69, 69, 69)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel3))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabelPaS)
                                            .addComponent(jLabelPeS)
                                            .addComponent(jLabelS)
                                            .addComponent(jLabelTs)))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(77, 77, 77)
                                        .addComponent(jLabelNbTotal)))))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblBonjour)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTitreDemande)
                    .addComponent(lblTitreDemande1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabelTs))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabelS))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabelPeS))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabelPaS))
                        .addGap(32, 32, 32)
                        .addComponent(jLabelNbTotal)))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addClient)
                    .addComponent(ficheClient))
                .addGap(18, 18, 18)
                .addComponent(btnSynchro)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ficheClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ficheClientActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ficheClientActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        if (!evt.getValueIsAdjusting()) {
            Object selectedValue = ((JList) evt.getSource()).getSelectedValue();
            clientSelect = null != selectedValue ? selectedValue.toString() : null;
        }
    }//GEN-LAST:event_jList1ValueChanged

    private void addClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addClientActionPerformed
        CreateClient cc = new CreateClient(user, this);
        cc.setVisible(true);

    }//GEN-LAST:event_addClientActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addClient;
    private javax.swing.JButton btnSynchro;
    private javax.swing.JButton ficheClient;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelNbTotal;
    private javax.swing.JLabel jLabelPaS;
    private javax.swing.JLabel jLabelPeS;
    private javax.swing.JLabel jLabelS;
    private javax.swing.JLabel jLabelTs;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblBonjour;
    private javax.swing.JLabel lblTitreDemande;
    private javax.swing.JLabel lblTitreDemande1;
    // End of variables declaration//GEN-END:variables

    public Utilisateur getUser() {
        return user;
    }

    public void setListClient() {
        UtilisateurServices uSrv = new UtilisateurServices();
        List<Client> clients = uSrv.getClients(user);
        this.user.setListClient(clients);

        int nbClient = clients.size();
        String[] names = new String[nbClient];
        int i = 0;
        for (Client client : clients) {
            names[i++] = client.getNom();
        }

        jList1.setListData(names);
    }

    public void setListSatisfaction() {
        UtilisateurServices uSrv = new UtilisateurServices();
        List<Satisfaction> satisfactions = uSrv.getSatisfaction(user);
        this.user.setListSatisfaction(satisfactions);
        int nbTotal = 0, nbTS = 0, nbS = 0, nbPeS = 0, nbPaS = 0;
        for (Satisfaction satisfaction : satisfactions) {
            switch (satisfaction.getTauxSatisfaction().getLibelle()) {
                case "Pas satisfait":
                    nbPaS++;
                    break;
                case "Peu satisfait":
                    nbPeS++;
                    break;
                case "Satisfait":
                    nbS++;
                    break;
                case "Très satisfait":
                    nbTS++;
                    break;
            }
            nbTotal++;
        }
        if (nbTotal == 0) {
            jLabelPaS.setText("0 %");
            jLabelPeS.setText("0 %");
            jLabelS.setText("0 %");
            jLabelTs.setText("0 %");
            jLabelNbTotal.setText("");
        } else {
            jLabelPaS.setText(String.valueOf(arrondiTaux(nbPaS, nbTotal)).toString() + " %");
            jLabelPeS.setText(String.valueOf(arrondiTaux(nbPeS, nbTotal)).toString() + " %");
            jLabelS.setText(String.valueOf(arrondiTaux(nbS, nbTotal)).toString() + " %");
            jLabelTs.setText(String.valueOf(arrondiTaux(nbTS, nbTotal)).toString() + " %");
            jLabelNbTotal.setText("Sur " + nbTotal + " commandes");
        }
    }

    public String getClientSelect() {
        return clientSelect;
    }

    public double arrondiTaux(int nbSat, int nbTot) {
        double tauxSat = (nbSat * 100) / nbTot;
        tauxSat = (double) Math.round(tauxSat * 1) / 1;

        return tauxSat;
    }
}
